<script setup>
import OImage from "../../setting/Images.vue";
import Color from "../../setting/Color.vue";
import PrizePopupSelect from "../../setting/PrizePopupSelect.vue";

const props = defineProps({
  config: {
    type: Object,
    required: true,
  },
  name: {
    type: String,
    default: "",
  },
  disabled: {
    type: Boolean,
    default: false,
  },
  group: {
    type: Array,
    default: () => [],
  },
});

const emits = defineEmits(["update:config", "change"]);

const form = ref({
  $position: { top: 0, left: 0 },
  $size: { width: 375, height: 401 },
  $removeable: true,
  bg: "./imagesStatic/activity/Jiugongge_bg.png",
  showBg: true,
  position: {
    top: 51,
    left: 30,
  },
  contentSize: { width: 315, height: 325 },
  btn: "./imagesStatic/activity/Jiugongge_btn.png",
  btnSize: {
    width: 100,
    height: 101,
  },
  prizeBgDefault: "./imagesStatic/activity/Jiugongge_default.png",
  prizeBgActive: "./imagesStatic/activity/Jiugongge_active.png",
  color: "#B12B01",
  grideSize: {
    width: 90,
    height: 93,
  },
  prizeSize: { width: 90, height: 90 },
  prizePosition: { top: 0, left: 0 },
});

const formRef = ref();

const detail = computed({
  get: () => {
    return Object.assign({}, form.value, props.config);
  },
  set: (val) => {
    emits("update:config", val);
    emits("change", val);
  },
});

const onChange = (val, type) => {
  detail.value = detail.value;
};

// 表单校验
const check = () => {
  return new Promise((resolve, reject) => {
    formRef.value.validate(async (valid, invalidFields) => {
      if (valid) {
        resolve();
      } else
        reject(
          `${props.name}校验未通过，${
            invalidFields[Object.keys(invalidFields)[0]][0].message
          }`
        );
    });
  });
};

onMounted(() => {
  nextTick(() => {
    onChange();
  });
});

defineExpose({ check });
</script>
<template>
  <el-form
    ref="formRef"
    :model="detail"
    :disabled="disabled"
    label-width="auto"
  >
    <PrizePopupSelect v-model="detail" :group="group" @change="onChange" />
    <Color v-model="detail.color" name="文字颜色" @change="onChange" />
    <OImage
      v-model="detail.bg"
      v-model:size="detail.$size"
      v-model:show="detail.showBg"
      name="背景图"
      @change="onChange"
    />
    <OImage
      v-model="detail.btn"
      v-model:size="detail.btnSize"
      name="抽奖按钮"
      @change="onChange"
    />
    <OImage
      v-model="detail.prizeBgDefault"
      v-model:size="detail.grideSize"
      name="默认奖品背景图"
      @change="onChange"
    />
    <OImage
      v-model="detail.prizeBgActive"
      v-model:size="detail.grideSize"
      name="中奖奖品背景图"
      @change="onChange"
    />
  </el-form>
</template>
